package com.guchenbo.example.thread;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @author guchenbo
 * @date 2024/12/13
 */
public class TestCompleteFuture {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CompletableFuture<Void> f = CompletableFuture.runAsync(() -> {
            System.out.println("run async has no answer");
        });
        CompletableFuture<Void> f2 = f.thenAccept((a) -> {
            System.out.println(a);
        });
        System.out.println(f2.get());


        CompletableFuture<String> f3 = CompletableFuture.supplyAsync(()->{
            System.out.println("supplyAsync");
            return "s1";
        });
        CompletableFuture<String> f4 = f3.thenApply((v) -> {
            System.out.println(v);
            return "s2";
        }).thenApply((v) -> {
            System.out.println(v);
            return "s3";
        }).thenApply((v) -> {
            System.out.println(v);
            return "s4";
        });
        System.out.println(f4.get());
    }
}
